package com.hunan.api.catv.mq.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.entity.enums.VedioReviewEnum;
import com.hunan.api.catv.entity.po.UgcNews;
import com.hunan.api.catv.entity.vo.UgcNewsVo;
import com.hunan.api.catv.service.biz.ugc.UgcNewsBiz;
import com.hunan.api.catv.service.impl.tencent_cloud.vm.VmService;
import com.hunan.api.catv.utils.JacksonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import com.tencentcloudapi.vm.v20200709.models.CreateVideoModerationTaskResponse;
import com.tencentcloudapi.vm.v20200709.models.DescribeTaskDetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: wangchao
 * @Date: 2020/11/24 15:55
 */
@Component
@Slf4j
public class UgcReleaseVedioConsumer {

    @Autowired
    UgcNewsBiz ugcNewsBiz;

    @Autowired
    RedisUtils redisUtils;

    private final VmService vmService;

    public UgcReleaseVedioConsumer(VmService vmService) {
        this.vmService = vmService;
    }


    //@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.UGC_RELEASE_VEDIO_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.UGC_RELEASE_VEDIO_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.UGC_RELEASE_VEDIO_ROUTINGKEY))
    @Transactional(rollbackFor = Exception.class)
    public void tasks(String msg) {
        log.info("[用户发布视频:{}]", msg);
        UgcNewsVo ugcNewsVo = JacksonUtils.parseObject(msg, UgcNewsVo.class);
        String[] vedioUrls = ugcNewsVo.getUrl().split(":");
        String newUrls = vedioUrls[2] + vedioUrls[3].split(",")[0];
        String ugcId = ugcNewsVo.getUgcId();
        try {
            CreateVideoModerationTaskResponse videoModerationTask = vmService.createVideoModerationTask(newUrls.substring(1, newUrls.length() - 1), ugcId);
            if (videoModerationTask != null && videoModerationTask.getResults() != null) {
                DescribeTaskDetailResponse describeTaskDetailResponse = vmService.describeTaskDetail(videoModerationTask.getResults()[0].getTaskId());
                UgcNews ugcNews = new UgcNews().selectOne(new QueryWrapper<UgcNews>().eq(UgcNews.UGC_ID, ugcId));
                boolean update = ugcNews.setReviewId(videoModerationTask.getResults()[0].getTaskId())
                        .setState(VedioReviewEnum.getCode(describeTaskDetailResponse != null ? describeTaskDetailResponse.getSuggestion() : ""))
                        .update(new QueryWrapper<UgcNews>().eq(UgcNews.UGC_ID, ugcId));
                redisUtils.hset(CacheName.releaseVideo, ugcNews.getUgcId(), JacksonUtils.toJsonObject(ugcNews));
                log.info("用户发布视频审核修改结果:{}", update);
            } else {
                //人工审核
                UgcNews ugcNews = new UgcNews().selectOne(new QueryWrapper<UgcNews>().eq(UgcNews.UGC_ID, ugcId));
                boolean update = ugcNews.setState(5).update(new QueryWrapper<UgcNews>().eq(UgcNews.UGC_ID, ugcId));
                redisUtils.hset(CacheName.releaseVideo, ugcNews.getUgcId(), JacksonUtils.toJsonObject(ugcNews));
                log.info("用户发布视频审核修改结果:{}", update);
            }
        } catch (Exception e) {
            log.error("[用户发布视频审核失败:{}]", e);
        }
    }


}
